package org.meetunexpectedly.config;

import org.meetunexpectedly.service.user.UserService;
import org.meetunexpectedly.util.RedisCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Description: web配置类
 * @Author: pupil
 * @Date: 2024/08/11 下午 7:11
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisCacheUtil redisCacheUtil;


    // 配置拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserInterceptor(userService,redisCacheUtil))
                .addPathPatterns("/admin/**","/authorize/**")
                .addPathPatterns("/meetunexpectedly/**")
                .excludePathPatterns("/meetunexpectedly/login/**","/meetunexpectedly/index/**","/meetunexpectedly/cdn/**","/meetunexpectedly/file/**");
    }

    // 配置跨域请求
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        String[] url = {"http://101.35.228.84:5378","http://127.0.0.1:8803","http://localhost:8803","http://127.0.0.1:5378", "http://localhost:5378"};
        registry.addMapping("/**") // 允许所有路径的CORS请求
                .allowedOrigins(url) // 允许的源
                .allowCredentials(true)  // 是否允许发送Cookie信息
                .allowedMethods("*")   // 允许跨域的方法，可以单独配置
                .allowedHeaders("*");  // 允许跨域的请求头，可以单独配置;
    }
}
